<?php

namespace App\Jobs;

use App\Models\User\WxUser;
use App\Wen\Utils\Settings;
use App\Wen\Utils\UserUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class UserRobotExpIncreaseJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $robot_count = WxUser::where('is_robot', '>', 0)->count();
        if($robot_count > 0){
            $online_count = (int)($robot_count * 0.04);
            $ids = WxUser::where('is_robot', '>', 0)->inRandomOrder()->limit($online_count)->get('id');
            if($ids){
                $ids->map(function ($v){
                    $id = $v->id;
                    if($id){
                        UserUtils::add_user_experience($id, 13, 20);
                    }
                });
            }
        }

        // Redis::sadd('robot:timed:post:active', $v->id);
        $robot_action_like_rate = _between_(Settings::get('robot_action_like_rate', 100), 0, 100) / 100;
        if($robot_action_like_rate > 0){
            $post_todo_set = Redis::smembers('robot:timed:post:active');
            if($post_todo_set){
                shuffle($post_todo_set);
                $post_todo_set_count = count($post_todo_set);
                $post_todo_set_count_20_rate = (int)($post_todo_set_count * 0.05);
                if($post_todo_set_count_20_rate > 0){

                    $last_time = now();
                    for ($i = 0; $i < $post_todo_set_count_20_rate; $i++){
                        $last_time = $last_time->addMinutes(mini_rand(1, 10) + $i * mini_rand(1, 5) );
                        $post_id = $post_todo_set[$i];
                        ImitatePostlikesStepJob::dispatch($post_id, mini_rand(1, 3))->delay($last_time);
                        $i ++;
                    }
                }
            }
            Cache::forget('robot:timed:post:active');
        }

        // Redis::sadd('robot:timed:comment:active', $v->id);
        $robot_action_comment_like_rate = _between_(Settings::get('robot_action_comment_like_rate', 100), 0, 100) / 100;
        if($robot_action_comment_like_rate > 0){
            $comment_todo_set = Redis::smembers('robot:timed:comment:active');
            if($comment_todo_set){
                shuffle($comment_todo_set);
                $comment_todo_set_count = count($comment_todo_set);
                $comment_todo_set_count_20_rate = (int)($comment_todo_set_count * 0.05);
                if($comment_todo_set_count_20_rate > 0){

                    $last_time = now();
                    for ($i = 0; $i < $comment_todo_set_count_20_rate; $i++){
                        $last_time = $last_time->addMinutes(mini_rand(1, 10) + $i * mini_rand(1, 5) );
                        $comment_id = $comment_todo_set[$i];
                        ImitateCommentlikesStepJob::dispatch($comment_id, mini_rand(1, 3))->delay($last_time);
                    }
                }
            }
            Cache::forget('robot:timed:comment:active');
        }


        // Redis::sadd('robot:timed:user:active', $user->id);
        $robot_action_comment_like_rate = _between_(Settings::get('robot_action_comment_like_rate', 100), 0, 100) / 100;
        if($robot_action_comment_like_rate > 0){
            $user_todo_set = Redis::smembers('robot:timed:user:active');
            if($user_todo_set){
                shuffle($user_todo_set);
                $user_todo_set_count = count($user_todo_set);
                $user_todo_set_count_count_20_rate = (int)($user_todo_set_count * 0.05);
                if($user_todo_set_count_count_20_rate > 0){
                    $last_time = now();
                    for ($i = 0; $i < $user_todo_set_count_count_20_rate; $i++){
                        $last_time = $last_time->addMinutes(mini_rand(1, 10) + $i * mini_rand(1, 5) );
                        $user_id = $user_todo_set[$i];
                        ImitateUserFollowsStepJob::dispatch($user_id, mini_rand(1, 3))->delay($last_time);
                    }
                }
            }
            Cache::forget('robot:timed:user:active');
        }

    }
}
